#include "addBinary.h"

std::string addBinary(std::string a, std::string b)
{
	if (a.length() < b.length())
	{
		std::string temp = a;
		a = b;
		b = temp;
	}

	int aLength = a.length() - 1;
	int bLength = b.length() - 1;

	int tempa = 0;
	int tempb = 0;
	int jinwei = 0;

	while (aLength >= 0)
	{
		if (bLength >= 0)
		{
			tempa = a[aLength] - '0';
			tempb = b[bLength] - '0';
			
			a[aLength] = ((tempa + tempb + jinwei) % 2) + '0';
			jinwei = (tempa + tempb + jinwei) / 2;
			aLength--;
			bLength--;
			continue;
		}

		tempa = a[aLength] - '0';
		
		a[aLength] = ((tempa + jinwei) % 2) + '0';
		jinwei = (tempa + jinwei) / 2;
		aLength--;
	}

	if (jinwei == 1)
	{
		a = "1" + a;
	}
	return a;
}
